<template>
  <div>
    <div class="container">
      <div class="box login">
        <div class="form-content">
          <h2>USER LOGIN</h2>
          <el-form class="form" :model="user" :rules="rules" ref="userForm">
            <el-form-item prop="username">
              <el-input size="medium" class="form-input" placeholder="username" prefix-icon="el-icon-user" v-model="user.username"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input size="medium" class="form-input" placeholder="password" prefix-icon="el-icon-lock" show-password
                        v-model="user.password"></el-input>
            </el-form-item>
            <el-form-item class="content-box-item">
              <el-button class="content-box-button" type="primary" :loading="loading" autocomplete="off" @click="login">
                <span v-if="!loading">登 录</span>
                <span v-else>登 录 中...</span>
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: "Login",
    data() {
      return {
        user: {},
        rules: {
          username: [
            {required: true, message: '请输入用户名', trigger: 'blur'},
            {min: 1, max: 20, message: '长度在 1 到 5 个字符', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '请输入密码', trigger: 'blur'},
            {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
          ],
        },
        loading:false,
      }
    },
    methods: {
      login() {
        this.$refs['userForm'].validate((valid) => {
          if (valid) {  // 表单校验合法
            this.loading = true   //加载显示
            setTimeout(() => {    //测试延时加载
              if(this.user.username === 'admin' && this.user.password === '123456'){
                this.$router.push("/")

                localStorage.setItem("username",this.user.username)
                //把用户名交给vuex管理
                this.$store.commit("getUser")

                //登录后改变登录状态
                this.$store.commit('getPermission', this.user.username)

                this.$message.success("登陆成功")
              }else {
                this.loading = false
                this.$message.error("登陆失败")
              }
            },1000)
          }
        });
      }
    }
  }
</script>

<style scoped>
  /*登录样式*/
  .container{
    position: absolute;
    top: 50%;
    left: 80%;
    transform: translate(-50%, -50%);
    width: 20rem;
    height: 26rem;
    perspective: 1500px;
    -webkit-perspective: 1500px;
    -moz-perspective: 1500px;
  }
  .box{
    position: absolute;
    top: 0;
    left: 0;
    width: 20rem;
    height: 22rem;
    backface-visibility: hidden;
    transition: all 0.5s;
    user-select: none;
  }
  .container .form-content{
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    background-color: rgba(255, 255, 255, 0.1);
    box-shadow:  0 3px 18px rgba(0, 134, 139, .5);
  }
  .container .form-content h2{
    width: 100%;
    text-align: center;
    margin-top: 20px;
    color: lightskyblue;
    font-size: 1.6rem;
  }
  .container .form{
    display: flex;
    flex-direction: column;
  }
  .form-input{
    width: 100%;
    margin: 5px 0;
  }
  .content-box-item{
    margin: 10px 0;
    text-align: center;
  }
  .content-box-button{
    width: 100%;
    height: 40px;
    border: 2px solid skyblue;
    font-size: 14px;
    margin-bottom: 20px;
  }
</style>
